package cc.yugu.yhdw.webserver.config;

import cc.yugu.yhdw.common.entity.AreaCache;
import cc.yugu.yhdw.common.tool.netty.BizFactory;
import cc.yugu.yhdw.common.tool.netty.DefaultBizFactory;
import cc.yugu.yhdw.common.tool.zk.ZookeeperEndpoint;
import cc.yugu.yhdw.webserver.comm.client.ServiceServerClientManager;
import cc.yugu.yhdw.webserver.comm.client.TerminalServerClientManager;
import cc.yugu.yhdw.webserver.constant.AppConstant;
import cc.yugu.yhdw.webserver.service.basic.SystemConfigService;
import cc.yugu.yhdw.webserver.utils.InstallUtils;
import com.whalin.MemCached.MemCachedClient;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletContext;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 * 配置文件实体
 *
 * @Date:Created in 2018/6/27 10:13
 * @Author:
 * @Modified By: zjn 2018/6/27 10:13
 */
public class AppConfig {
    private ServletContext servletContext;
    private String mapKey = "E17e88c82dfacce1c0c2ff4ad70a5bd1";
    // 创建一个可以存放100个String对象的内存空间
    public List<String> mobileMessageTemplateSql = new ArrayList<String>(100);
    public List<String> smsConfigSql = new ArrayList<String>(10);
    public List<String> agentConfigSql = new ArrayList<String>(100);

    public String staticUrl;
    public String zkUrl;
    public ZookeeperEndpoint zkClient;
    public String version;
    public File tempDir;
    public File appDir;
    public File portraitDir;
    public String contextPath;
    public String uploadSalt;
    public TerminalServerClientManager terminalServerClientManager = new TerminalServerClientManager(this);
    public ServiceServerClientManager serviceServerClientManager = new ServiceServerClientManager(this);
    public ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2 + 1);
    public BizFactory bizFactory = new DefaultBizFactory();
    /*  依赖注入  */
    @Autowired
    SystemConfigService systemConfigService;

    @Autowired
    public AreaCache areaCache;
    @Autowired
    public MemCachedClient memCachedClient;
    /**
    * 初始化路径名 及实例创建
    *
    * @Param: [sc]
    * @Return: void
    * @Author By: zjn 2018/6/27  10:57
    */
    public final void init(ServletContext sc) {
        this.servletContext = sc;
        this.contextPath = sc.getContextPath();
        this.appDir = new File(sc.getRealPath(""));
        this.tempDir = new File(appDir, AppConstant.PATH_TEMP);
        //从父抽象路径名(appDir)和子路径名字符串创建新的<code>File</code>实例
        this.portraitDir = new File(appDir, AppConstant.PATH_PORTRAIT);

        try {
            initConfig(sc);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /**
    * 初始化配置信息
    *
    * @Param: [sc]
    * @Return: void
    * @Author By: zjn 2018/6/27  10:49
    */
    protected void initConfig(ServletContext sc) throws Exception {
        tempDir = getFile(AppConstant.PATH_TEMP);

        systemConfigService.initConfig();

        //初始化SQL
        File file = new File(servletContext.getRealPath("/WEB-INF/db.sql"));
        List<String> list = InstallUtils.formatSql(FileUtils.readFileToString(file, "utf-8"));
        for (String sql : list) {
            if (sql.startsWith("insert into bas_mobile_message_template")) {
                mobileMessageTemplateSql.add(sql);
            }
        }

        //运营商广告配置
        list = FileUtils.readLines(file, "utf-8");
        for (String sql : list) {
            if (sql.startsWith("--insert into yms_agent_config")) {
                agentConfigSql.add(sql.replace("--insert", "insert").replace(");", ")"));
            }
        }
    }

    public String getRealPath(String webPath) {
        return servletContext.getRealPath(webPath);
    }

    public File getFile(String webPath) {
        // 从父抽象路径名和子路径名字符串创建新文件实例。
        return new File(appDir, webPath);
    }

    public String getMapKey() {
        return mapKey;
    }

    public void setZkUrl(String zkUrl) {
        this.zkUrl = zkUrl;
    }

    public void setStaticUrl(String staticUrl) {
        this.staticUrl = staticUrl;
    }

    public String getStaticUrl() {
        return staticUrl;
    }

    public AreaCache getAreaCache() {
        return areaCache;
    }

    public void setAreaCache(AreaCache areaCache) {
        this.areaCache = areaCache;
    }

    public String getUploadSalt() {
        return uploadSalt;
    }

    public void setUploadSalt(String uploadSalt) {
        this.uploadSalt = uploadSalt;
    }

    public MemCachedClient getMemCachedClient() {
        return memCachedClient;
    }

    public void setMemCachedClient(MemCachedClient memCachedClient) {
        this.memCachedClient = memCachedClient;
    }
}
